package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.ls.LSInput;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.adapter.AdapterListProduct1.ViewHolder;
import apps.softtech.fastmarket.model.SaleTopic;

/**
 * 
 * @author MUI
 * 
 */
public class AdapterSaleHistory extends BaseAdapter {

	private Context mContext;
	Activity activity;
	LayoutInflater inflater;
	List<SaleTopic> listSaleTopic = new ArrayList<SaleTopic>();
	SaleTopic topic;

	public AdapterSaleHistory(Context context, List<SaleTopic> listSaleTopic) {
		mContext = context;
		this.listSaleTopic = listSaleTopic;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if (listSaleTopic == null || listSaleTopic.equals(null)
				|| listSaleTopic.size() == 0){
			return 1;
		}
		return listSaleTopic.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = new ViewHolder();
		if (listSaleTopic == null || listSaleTopic.equals(null)
				|| listSaleTopic.size() == 0) {
			convertView = inflater.inflate(R.layout.search_no_result, null);
			return convertView;
		}
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_history_sale, null);
			initViewHolder(convertView, viewHolder);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		topic = listSaleTopic.get(position);
		
		viewHolder.tvNameTopic.setText(topic.getTopic());
		viewHolder.tvOffer.setText("Date: " + topic.getCreatedDate() + " Price: " + topic.getOfferPrice());
		
		return convertView;
	}

	private void initViewHolder(View v, ViewHolder holder) {
		holder.tvNameTopic = (TextView) v.findViewById(R.id.tvTopic);
		holder.tvComment = (TextView) v.findViewById(R.id.tvComment);
		holder.tvOffer = (TextView) v.findViewById(R.id.tvOffer);
	}

	static class ViewHolder {
		TextView tvNameTopic;
		TextView tvComment;
		TextView tvOffer;
	}

}
